package com.quanquan.user.controller;

import DTO.PageDTO;
import com.quanquan.user.dto.SignInDTO;
import com.quanquan.user.entity.SignIn;
import com.quanquan.user.exception.SignInRuleException;
import com.quanquan.user.request.SignInRequestBody;
import com.quanquan.user.service.SignInRuleService;
import com.quanquan.user.service.SignInService;
import enums.ResCodeEnum;
import enums.UserResCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import request.PageRequestBody;
import utils.StringUtils;
import web.response.ResultBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 功能描述:签到管理
 *
 * @ClassName: SignInController
 * @Author: xiaoqianh丶
 * @Date: 2019-04-05 18:52
 * @Version: V1.0
 */
@Controller
@RequestMapping("/sign")
public class SignInController {

    private static final String SIGN_IN = "_signIn";
    public static final String REWARD_SIGN = "reward_sign";

    private final SignInService signInService;
    private final StringRedisTemplate redisTemplate;
    private final SignInRuleService signInRuleService;
    @Autowired
    public SignInController(SignInService signInService, StringRedisTemplate redisTemplate, SignInRuleService signInRuleService) {
        this.signInService = signInService;
        this.redisTemplate = redisTemplate;
        this.signInRuleService = signInRuleService;
    }

    /**
     * 签到
     * @param requestBody
     * @return
     */
    @PostMapping("/check_sign")
    @ResponseBody
    public ResultBody cheackSign (@RequestBody SignInRequestBody requestBody){
        String userId = requestBody.getUserId();
        String signKey= userId + SIGN_IN;
        String signResult = redisTemplate.opsForValue().get(signKey);
        Map map =  new HashMap<>();
        if(StringUtils.isNotEmpty(signResult)){
            return new ResultBody(UserResCodeEnum.SIGN_IN_FAIL.getCode(), UserResCodeEnum.SIGN_IN_FAIL.getMsg(),false);
        }
        int totalPointsDay = 0;
        try {
            totalPointsDay = signInService.signInResult(userId);
        } catch (SignInRuleException e) {
            e.printStackTrace();
            return new ResultBody(UserResCodeEnum.SIGN_IN_FAIL.getCode(), UserResCodeEnum.SIGN_IN_FAIL.getMsg(),totalPointsDay);

        }
        map.put("totalPointsDay", "签到成功获得"+totalPointsDay+"积分。");

        //查询redis看是否连续签到
        String s = redisTemplate.opsForValue().get(REWARD_SIGN);
        if(StringUtils.isNotEmpty(s)){
            if(s.equals("1")){
                map.put("totalPointsDay","签到成功获得"+totalPointsDay+"积分，恭喜你额外一张优惠券，请到券包查看。");
            }
        }
        if(totalPointsDay ==0){
            return new ResultBody(UserResCodeEnum.SIGN_IN_SUCCESS.getCode(), UserResCodeEnum.SIGN_IN_SUCCESS.getMsg(),map);
        }

        return new ResultBody(UserResCodeEnum.SIGN_IN_SUCCESS.getCode(), UserResCodeEnum.SIGN_IN_SUCCESS.getMsg(),map);
    }

    @PostMapping("/query")
    @ResponseBody
    public ResultBody findAllSignIn (@RequestBody SignInRequestBody requestBody){
        String userId = requestBody.getUserId();
        List<SignIn> allPersionSignIn = signInService.findAllPersionSignIn(userId);
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg().toString(),allPersionSignIn);

    }

    @PostMapping("/first_check")
    @ResponseBody
    public ResultBody firstCheck (@RequestBody SignInRequestBody requestBody){
        String userId = requestBody.getUserId();
        String signKey= userId + SIGN_IN;
        String signResult = redisTemplate.opsForValue().get(signKey);
        if(StringUtils.isNotEmpty(signResult)){
            return new ResultBody(UserResCodeEnum.SIGN_IN_SUCCESS.getCode(), UserResCodeEnum.SIGN_IN_SUCCESS.getMsg().toString(),0);
        }
        return new ResultBody(UserResCodeEnum.SIGN_IN_SUCCESS.getCode(), UserResCodeEnum.SIGN_IN_SUCCESS.getMsg().toString(),1);

    }

    @PostMapping("/query_condition")
    @ResponseBody
    public ResultBody firstCheck (@RequestBody PageRequestBody requestBody){
        PageDTO<SignInDTO> pageDTO = signInService.pagedList(requestBody);
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg(), pageDTO);
    }

}
